﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace File_Operate_Program
{
    public partial class FrmCSV_Editor_AddRows : Form
    {
        public string AddRowsCount { get; set; }
        private string Notes = "请在此输入待添加的行数！";
        public FrmCSV_Editor_AddRows()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void FrmCSV_Editor_AddRows_Load(object sender, EventArgs e)
        {
            TxtAddRowsCount.MaxLength = 5;
            this.BtnOK.Click += new System.EventHandler(this.BtnOK_Click);
            this.BtnCancel.Click += new System.EventHandler(this.BtnCancel_Click);
            this.TxtAddRowsCount.Enter += new System.EventHandler(this.TxtAddRowsCount_Enter);
            this.TxtAddRowsCount.Leave += new System.EventHandler(this.TxtAddRowsCount_Leave);
            this.TxtAddRowsCount.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TxtAddRowsCount_KeyPress);
        }

        private void BtnOK_Click(object sender, EventArgs e)
        {
            AddRowsCount = TxtAddRowsCount.Text;
            this.DialogResult = DialogResult.OK;
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void TxtAddRowsCount_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(TxtAddRowsCount.Text))
            {
                TxtAddRowsCount.ForeColor = Color.DarkGray;
                this.TxtAddRowsCount.Text = Notes;
            }
        }

        private void TxtAddRowsCount_Enter(object sender, EventArgs e)
        {
            if (TxtAddRowsCount.Text == Notes)
            {
                TxtAddRowsCount.ForeColor = Color.Black;
                this.TxtAddRowsCount.Text = "";
            }
        }

        private void TxtAddRowsCount_KeyPress(object sender, KeyPressEventArgs e)
        {
            //数字0~9所对应的keychar为48~57
            e.Handled = true;
            //输入0-9
            if ((e.KeyChar >= 47 && e.KeyChar <= 58) || e.KeyChar == 8)
            {
                e.Handled = false;
            }
            else if (e.KeyChar == (Char)Keys.Enter)
            {
                BtnOK_Click(null,null);
            }
        }
    }
}
